<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class academic extends User_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    function __construct() {
        parent::__construct();
        $this->load->model("database");
    }

    public function index() {
        $this->load->view('academic');
    }

    /**
     * Show Different Views of Page
     * 
     * Including academic view, industry view and other view
     */
    function showAcademic() {
        $this->load->view("academic");
    }

    function showIndustry() {
        
    }

    function showOthers() {
        
    }

    /**
     * Load News View
     * 
     * This method gets news data from database and load news views with data
     */
    function showNews() {
        $query = $this->database->getNews();
        $data = array();
        $i = 0;
        foreach ($query as $row) {
            $data[$i]['news'] = $row->news;
            $data[$i]['date'] = $row->date;
            $i++;
        }
        $this->load->view('news', array('data' => $data));
    }

    function showAbout() {
        $this->load->view("about");
    }

    function showTutorials() {
        $this->load->view("tutorials");
    }

    function showPeople() {
        $this->load->view("people");
    }

    function showPapers() {
        $query = $this->database->getPapers();
        $papers = $query->result_array();
        $this->load->view('papers', array('data' => $papers));
    }

    function showLinks() {
        $type = $this->input->get_post('type');
        $links = $this->database->getLinks();
        $data = $links->result_array();
        $headers = array();
        $i = 0;
        foreach ($links->result() as $link) {
            if (in_array($link->header, $headers)) {
                continue;
            }
            $headers[$i] = $link->header;
            $i++;
        }
        $this->load->view("links", array('headers' => $headers, 'data' => $data, 'type' => $type));
    }

    function showTools() {
        
    }

    function showFeedbacks() {
        $this->load->view("feedback");
    }

    function showPosts() {
//        $this->load->library('pagination');
//        $config['base_url'] = base_url() . 'academic/showPosts';
//        $config['per_page'] = 5;
//        $config['num_links'] = 5;
//        $config['total_rows'] = $this->db->get('question')->num_rows();
//
//        $this->pagination->initialize($config);
//        $data['query'] = $this->db->get('question', $config['per_page'], $this->uri->segment(3));

        $data = array();
        $posts = $this->database->getPosts();
        foreach ($posts as $post) {
            $data[$post->id]['id'] = $post->id;
            $data[$post->id]['title'] = $post->title;
            $data[$post->id]['question'] = $post->question;
            $answers = $this->database->getAnswer($post->id);
            $data[$post->id]['ans_num'] = $answers->num_rows();
            $data[$post->id]['date'] = $post->timestamp;
            $data[$post->id]['category'] = $post->category;
            $data[$post->id]['affiliation'] = $post->affiliation;
            $name = $this->get_userFullName($post->userid);
            $data[$post->id]['name'] = $name;
        }
        $this->load->view('posts', array('data' => $data));
    }

    function viewPost() {
        if ($this->session->userdata('validated')) {
            $current_username = $this->get_username();
            $current_userid = $this->get_userID($current_username);
            $userType = $this->get_userType($current_username);
        }

        $captcha = $this->get_captcha();
        $id = $this->input->get_post('id');
        $post = $this->database->getPostByID($id);
        $userid = $post['userid'];
        $name = $this->get_userFullName($userid);
        $ans = $this->database->getAnswer($id)->result_array();
        $ans_num = $this->database->getAnswer($id)->num_rows();
        if ($this->session->userdata('validated')) {
            $this->load->view('show_post', array('userType' => $userType, 'userid' => $current_userid, 'post' => $post, 'ans' => $ans, 'ans_num' => $ans_num, 'name' => $name, 'captcha' => $captcha));
        } else {
            $this->load->view('show_post', array('post' => $post, 'ans' => $ans, 'ans_num' => $ans_num, 'name' => $name, 'captcha' => $captcha));
        }
    }

    function submitAnswer() {
        $questionID = $this->input->get_post('questionID');
        $name = $this->input->get_post('name');
        $email = $this->input->get_post('email');
        $answer = $this->input->get_post('answer');
        if ($this->database->insertAnswer($questionID, $name, $email, $answer)) {
            echo "Success";
            return true;
        }
        return false;
    }

    function submitFeedback() {
        $name = $this->input->get_post('name');
        $affiliation = $this->input->get_post('affiliation');
        $email = $this->input->get_post('email');
        $category = $this->input->get_post('category');
        $comment = $this->input->get_post('comment');
        $query = $this->database->insertFeedback($name, $affiliation, $email, $category, $comment);
        echo $query;
        return false;
    }

    function viewFile($filename, $filapath) {
        
    }

}
